//  我的-部门管理-上传面试群二维码
const db = wx.cloud.database({
  env: 'cloud1-8g54d6xb74e96e7d'
})
const app = getApp();
Page({
  /* 页面的初始数据 */
  data: {
    oldSecretKey:"",
    updateStatus:false,
    department: [],
    departmentName: '请选择部门',
    department_index: '',
    picUrl: '', //本地临时图片路径
    secretKey:"",
  },

  // methods
  _ChooseDepartment: function (e) {
    /* console.log('picker发送选择改变，携带下标为', e.detail.value) */
    console.log('picker发送选择改变，携带值为', this.data.department[e.detail.value])
    this.setData({
      departmentName: '',
      department_index: e.detail.value,
      dname: this.data.department[e.detail.value],
      n: 1
    })
  },

  /* 选择图片 */
  upimage() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success: res => {
        console.log('页面获取图片成功')
        /* var filePath=res.tempFiles[0].tempFilePath.slice(11)//slice函数-截取字符串（路径=字符串）
        console.log('截取临时路径：',filePath), */
        this.setData({
          picUrl: res.tempFiles[0].tempFilePath
        })
      }
    })
  },

  /* 点击上传按钮 */
  async handleUpload() {
    let that = this;
    //健壮性校验：防止picUrl为空
    console.log(that.data.picUrl)
    if(!that.data.picUrl){
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请选择新上传的二维码',
        cancelColor: 'cancelColor',
      })
      return
    }
    if(this.getFileExtension(that.data.picUrl)!=="jpg"){
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请上传JPG格式的图片',
        cancelColor: 'cancelColor',
      })
      return
    }
    wx.showLoading({ //提示信息“上传中···”
      title: '上传中',
    })
    /* 上传 */
    wx.uploadFile({
      url: getApp().data.domain + '/uploadPic',
      filePath:that.data.picUrl,
      name:"file",
      formData:{
        departmentId:getApp().data.personInfo.departmentId,
      },
      success:function(res){
        console.log(res.data)
        getApp().showSuccess("上传成功！")
        wx.hideLoading()
      },
      fail:function(err){
        console.error(err)
        getApp().showError("上传失败！")
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: '请选择新上传的二维码',
          cancelColor: 'cancelColor',
        })
        wx.hideLoading()
      }
    })
  },
//截取文件名后缀
getFileExtension(filename) {
    // 从文件名的最后一个'.'字符开始截取子字符串
    return filename.slice(((filename.lastIndexOf(".") - 1) >>> 0) + 2);
  },

  // 复制密钥
  copySecretKey(){
    let that=this;
    wx.setClipboardData({
      // this.data.copyText  需要拷贝的文字
      data: that.data.secretKey, 
      success (res) {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },

  // 点击“修改密钥”
  updateSecretKey(){
    this.setData({
      updateStatus:true
    })
    wx.showToast({
      title: '可编辑',
      icon:"none"
    })
  },

  // 输入密钥更新model层data数据域里的secretKey
  handleInputSecretKey(e){
    console.log(e);
    this.setData({
      secretKey:e.detail.value
    })
  },

  // 保存密钥
  saveSecretKey(){
    let that=this;
    if(that.data.oldSecretKey==that.data.secretKey){
      wx.showToast({
        title: '新旧密钥一致，无需修改',
        icon:"none"
      });
      that.setData({
        updateStatus:false
      })
      return;
    }
    if(that.data.secretKey.length<8 || that.data.secretKey.length>16){
      wx.showToast({
        title: '请输入8-16位字符串',
        icon:"error"
      });
      return;
    }

    db.collection("InterviewGroup").where({
      title:that.data.departmentName
    }).update({
      data:{
        secretKey:that.data.secretKey
      },
      success:res=>{
        wx.showToast({
          title: '保存成功',
        })
        that.setData({
          updateStatus:false,
          oldSecretKey:that.data.secretKey,
        })
      },
      fail:res=>{
        wx.showToast({
          title: '保存失败',
          icon:"error"
        })
      }
    })
    this.setData({
      updateStatus:false
    })
  },

 //------------------------------------------------------------ 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that=this;
    var personInfo = getApp().data.personInfo
    this.setData({
      dname: personInfo.departmentId,
      departmentName: getApp().data.departmentArray[personInfo.departmentId - 1] ,
      picUrl:getApp().data.baseDomain + "/pic/" + personInfo.departmentId + ".jpg"
    });
  },
})